package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.AccountMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.Account;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.AccountService;
import com.tpp.threat_perception_platform.service.RiskAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RiskAssessmentService riskAssessmentService;// 注入统一风险评估服务

    @Transactional
    @Override
    public int saveAccount(Account account) {

        // 设置公共属性
        account.setCreateTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return accountMapper.insertSelective(account);
    }

    @Override
    public List<Account> getAccountsByMacAddress(String macAddress) {
        return accountMapper.findAllByMacAddress(macAddress);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        accountMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult accountList(MyParam param) {
        // 设置分页参数
        PageHelper.startPage(param.getPage(), param.getLimit());

        // 查询所有账号数据
        List<Account> dataList = accountMapper.findAll(param);

        // 风险评估逻辑（注入 RiskAssessmentService）
        for (Account account : dataList) {
            String riskLevel = riskAssessmentService.assessAccountRisk(account);
            account.setRiskLevel(riskLevel);

            // 可选：写回数据库
            // accountMapper.updateRiskLevelByMacAddress(account.getMacAddress(), riskLevel);
        }

        // 构建分页响应
        PageInfo<Account> pageInfo = new PageInfo<>(dataList);

        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

}
